#include "timerbtn.h"
#include<QMouseEvent>

TimerBtn::TimerBtn(QWidget *parent): QPushButton(parent), _counter(10)
{
    _timer = new QTimer(this);
    connect(_timer, &QTimer::timeout, [this]() {
        _counter--;
        if(_counter < 0) {
            _timer->stop();
            _counter = 10;
            this->setText("获取验证码");
            this->setEnabled(true);
            return true;
        }
        this->setText(QString::number(_counter));
    });
}

TimerBtn::~TimerBtn()
{
    _timer->stop();
}

void TimerBtn::mouseReleaseEvent(QMouseEvent *e)
{
    if(e->button() == Qt::LeftButton){
        this->setEnabled(false);
        this->setText(QString::number(_counter));
        _timer->start(1000);
        emit clicked(); //这是为了适配低版本，因为自定义的事件可能会导致父类不会自动触发clicked
    }
    QPushButton::mouseReleaseEvent(e);
}
